package main.java.DMDE.view;

import main.java.ERMDE.jgraph.extensions.CustomView;

import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxICell;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;

public class CustomGraph extends mxGraph {
    
    public static int ARROW_SPACING = 30;
    public static int ARROW_SIZE = 60;
    
    public CustomGraph() {
	this.cellsEditable = false;
	this.cellsResizable = true;
	this.cellsBendable = true;
	this.cellsCloneable = false;
	this.cellsDeletable = true;
	this.cellsDisconnectable = true;
	this.constrainChildren = true;
	this.extendParents = true;
	this.extendParentsOnAdd = true;
	this.setConnectableEdges(true);
	this.setAutoSizeCells(true);
    }

    @Override
    protected mxGraphView createGraphView() {
	return new CustomView(this);
    }

    @Override
    public boolean isCellLocked(Object cell) {
	return this.getModel().isEdge(cell);
    }

    @Override
    public boolean isCellMovable(Object cell) {
	return !getModel().isVertex(getModel().getParent(cell));
    }
    
    public void cellSizeUpdated(Object cell, boolean ignoreChildren) {
    	super.cellSizeUpdated(cell, false);
    }
    
    public void cellsResized(Object[] cells, mxRectangle[] bounds) {
    	super.cellsResized(cells, bounds);
    	for (Object cell : cells) {
    		resizeRecursive((mxCell)cell);
    	}
    }
        
    private void resizeRecursive(mxCell parent) {
    	for (int i = 0; i < parent.getChildCount(); i++) {
    		mxICell previous = null;
//    		if (i > 0) {
//    			previous = parent.getChildAt(i - 1);
//    		}
    		mxICell child = parent.getChildAt(i);
//    		if (previous != null) {
//    			child.getGeometry().setY(previous.getGeometry().getY() + previous.getGeometry().getHeight());
//    		}
    		child.getGeometry().setHeight(0.50 * parent.getGeometry().getHeight());
    		child.getGeometry().setWidth(parent.getGeometry().getWidth());
    		if ( i > 0) {
    			child.getGeometry().setY(parent.getGeometry().getHeight() * 0.50);
    		}
    	}
    }
    
    public boolean isAllowOverlapParent(Object cell) {
    	return !(cell instanceof CustomGraph )|| false;
    }

}
